package controllers

import (
	"fmt"
	"my-go-project/model"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type passage_like struct {
	MessageType  string         `json:"Messagetype"`
	UserInfo     model.UserMini `json:"UserInfo"`
	PassageID    int            `json:"PassageID"`
	PassageTitle string         `json:"PassageTitle"`
	PassageType  string         `json:"PassageType"`
}

type comment_like struct {
	MessageType string         `json:"Messagetype"`
	UserInfo    model.UserMini `json:"UserInfo"`
	PassageID   int            `json:"PassageID"`
	PassageType string         `json:"PassageType"`
	CommentID   int            `json:"CommentID"`
	Comment     string         `json:"Comment"`
}

type passage_comment struct {
	MessageType  string         `json:"MessageType"`
	UserInfo     model.UserMini `json:"UserInfo"`
	PassageID    int            `json:"PassageID"`
	PassageTitle string         `json:"PassageTitle"`
	PassageType  string         `json:"PassageType"`
	CommentID    int            `json:"CommentID"`
	Comment      string         `json:"Comment"`
}

type comment_comment struct {
	MessageType string         `json:"MessageType"`
	UserInfo    model.UserMini `json:"UserInfo"`
	PassageID   int            `json:"PassageID"`
	PassageType string         `json:"PassageType"`
	CommentID   int            `json:"CommentID"`
	Comment     string         `json:"Comment"`
	Reply       string         `json:"Reply"`
}

type check_notice struct {
	MessageType string         `json:"MessageType"`
	UserInfo    model.UserMini `json:"UserInfo"`
	AdoptID     int            `json:"AdoptID"`
	PassageID   int            `json:"PassageID"`
	PassageType string         `json:"PassageType"`
}

type notice_common struct {
	MessageType string         `json:"MessageType"`
	UserInfo    model.UserMini `json:"UserInfo"`
	PassageID   int            `json:"PassageID"`
	PassageType string         `json:"PassageType"`
}

type request_success struct {
	MessageType string                `json:"MessageType"`
	UserInfo    model.UserWithContact `json:"UserInfo"`
	PassageID   int                   `json:"PassageID"`
	PassageType string                `json:"PassageType"`
}

type comment_notice struct {
	MessageType  string                 `json:"MessageType"`
	UserInfo     model.UserMini         `json:"UserInfo"`
	PassageID    int                    `json:"PassageID"`
	PassageType  string                 `json:"PassageType"`
	AdoptComment model.AdoptCommentMini `json:"AdoptComment"`
}

func GetLikeMessage(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	messages, err := model.GetLikeMessageByID(id)
	var likeMessages []interface{}
	// 对mesasges进行特例处理
	for _, message := range *messages {
		if message.Type == "comment_like" {
			var m comment_like
			// 获取message部分数据
			m.MessageType = "comment_like"
			m.PassageID = message.PassageID
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)
			m.CommentID = message.CommentID
			m.Comment, _ = model.GetCommentByID(m.CommentID)
			// 获取user部分数据
			m.UserInfo, _ = model.GetUserMiniByID(message.SenderID)

			likeMessages = append(likeMessages, m)
		} else if message.Type == "passage_like" {
			var m passage_like
			// 获取message部分数据
			m.MessageType = "passage_like"
			m.PassageID = message.PassageID
			m.PassageTitle, _ = model.GetTitleByID(message.PassageID)
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)
			// 获取user部分数据
			m.UserInfo, _ = model.GetUserMiniByID(message.SenderID)

			likeMessages = append(likeMessages, m)
		}
	}

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	fmt.Println(likeMessages)
	c.JSON(http.StatusOK, likeMessages)
}

func GetCommentMessage(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	messages, err := model.GetCommentMessageByID(id)
	var commentMessages []interface{}
	// 对mesasges进行特例处理
	for _, message := range *messages {
		if message.Type == "passage_comment" {
			var m passage_comment
			// 获取message部分数据
			m.MessageType = "passage_comment"
			m.PassageID = message.PassageID
			m.PassageTitle, _ = model.GetTitleByID(message.PassageID)
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)

			m.CommentID = message.CommentID
			m.Comment, _ = model.GetCommentByID(m.CommentID)
			// 获取user部分数据
			m.UserInfo, _ = model.GetUserMiniByID(message.SenderID)

			commentMessages = append(commentMessages, m)
		} else if message.Type == "comment_comment" {
			// 获取message部分数据
			var m comment_comment
			m.MessageType = "comment_comment"
			m.PassageID = message.PassageID
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)
			cComment, _ := model.GetCommentAllByID(message.CommentID)
			pComment, _ := model.GetCommentByID(cComment.CommentReplyID)
			m.CommentID = message.CommentID
			m.Comment = pComment
			m.Reply = cComment.CommentContent
			// 获取user部分数据
			m.UserInfo, _ = model.GetUserMiniByID(message.SenderID)

			commentMessages = append(commentMessages, m)
		}
	}

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	fmt.Println(commentMessages)
	c.JSON(http.StatusOK, commentMessages)
}

func GetAdoptionMessage(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	messages, err := model.GetAdoptMessageByID(id)
	var adoptMessages []interface{}
	for _, message := range *messages {
		switch message.Type {
		case "check_notice":
			var m check_notice
			m.MessageType = "check_notice"
			m.UserInfo, _ = model.GetUserMiniByID(message.SenderID)
			m.AdoptID = message.AdoptID
			m.PassageID = message.PassageID
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)
			adoptMessages = append(adoptMessages, m)
		case "request_fail":
			var m notice_common
			m.MessageType = "request_fail"
			m.UserInfo, _ = model.GetUserMiniByID(message.SenderID)
			m.PassageID = message.PassageID
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)
			adoptMessages = append(adoptMessages, m)
		case "request_success":
			var m request_success
			m.MessageType = "request_success"
			m.UserInfo, _ = model.GetUserWithContactByID(message.SenderID)
			m.PassageID = message.PassageID
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)
			adoptMessages = append(adoptMessages, m)
		case "adopt_fail":
			var m notice_common
			m.MessageType = "adopt_fail"
			m.UserInfo, _ = model.GetUserMiniByID(message.SenderID)
			m.PassageID = message.PassageID
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)
			adoptMessages = append(adoptMessages, m)
		case "adopt_success":
			var m check_notice
			m.MessageType = "adopt_success"
			m.UserInfo, _ = model.GetUserMiniByID(message.SenderID)
			m.PassageID = message.PassageID
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)
			m.AdoptID = message.AdoptID
			adoptMessages = append(adoptMessages, m)
		case "comment_notice":
			var m comment_notice
			m.MessageType = "comment_notice"
			m.UserInfo, _ = model.GetUserMiniByID(message.SenderID)
			m.PassageID = message.PassageID
			m.PassageType, _ = model.GetPassageTypeByID(message.PassageID)
			m.AdoptComment, _ = model.GetAdoptCommentMiniByID(message.AdoptID, message.SenderID)
			adoptMessages = append(adoptMessages, m)
		}
	}
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	fmt.Println(adoptMessages)
	c.JSON(http.StatusOK, adoptMessages)
}
